#pragma once

class MessageWriter
{
public:
	MessageWriter(char* pBuffer,uint32 nLength)
	{
		m_pBuffer = pBuffer;
		m_nLength = nLength;
		m_nWritePos = 0;
	}
	
	template<class T>
	bool Write(T& nValue)
	{
		if ( m_nWritePos + sizeof(nValue) <= m_nLength )	
		{
			*((T*)&m_pBuffer[m_nWritePos]) = nValue;
			m_nWritePos += sizeof(T); 
			return true;
		}
		else
		{
			return false;
		}
	}

	bool WriteBuffer(const char* pBuffer,uint32 nLength)
	{
		if ( m_nWritePos + nLength <= m_nLength )	
		{
			memcpy(&m_pBuffer[m_nWritePos],pBuffer,nLength);
			m_nWritePos += nLength;
			return true;
		}
		else
		{
			return false;
		}
	}
	
	bool WriteString(std::string& sValue)
	{
		uint32 nLength = (uint32)sValue.size();
		if ( Write(nLength) )
		{
			return WriteBuffer(sValue.c_str(),nLength);
		}
		else
		{
			return false;
		}
	}

	char*	Base(){ return &m_pBuffer[m_nWritePos];}
	uint32  Length() {return m_nLength;}
	uint32  WriteLength(){return m_nWritePos;}
	void    WriteLength(uint32 nLength){ m_nWritePos += nLength;}
private:
	char*	m_pBuffer;
	uint32	m_nLength;
	uint32	m_nWritePos;
};